/**
 * 场景类
 */
// 挡板对象
class Paddle {
    constructor(_main) {
        let p = {
            x: _main.paddle_x,
            y: _main.paddle_y,
            w: 102,
            h: 22,
            speed: 10,
            ballSpeedMax: 8,
            image: imageFromPath(allImg.paddle),
            isLeftMove: true,
            isRightMove: true
        }
        Object.assign(this, p)
    }
    moveLeft() {
        this.x -= this.speed
    }
    moveRight() {
        this.x += this.speed
    }
    // 小球、挡板碰撞检测
    collide(ball) {
        let b = ball
        let p = this
        if (Math.abs((b.x + b.w/2) - (p.x + p.w/2)) < (b.w + p.w)/2 &&
            Math.abs((b.y + b.h/2) - (p.y + p.h/2)) < (b.h + p.h)/2) {
            return true
        }
        return false  
    }
    // 计算小球、挡板碰撞后x轴移动速度值
    collideRange(ball) {
        let b = ball
        let p = this
        let rangeX = 0
        rangeX = (p.x + p.w/2) - (b.x + b.w/2)
        if (rangeX < 0) {
            // 小球撞击挡板左侧
            return rangeX / (b.w/2 + p.w/2) * p.ballSpeedMax
        } else if (rangeX > 0) {
            // 小球撞击挡板右侧
            return rangeX / (b.w/2 + p.w/2) * p.ballSpeedMax
        }
    }
}
// 小球对象
class Ball {
    constructor(_main) {
        let b = {
            x: _main.ball_x,
            y: _main.ball_y,
            w: 18,
            h: 18,
            speedX: 1,
            speedY: 5,
            image: imageFromPath(allImg.ball),
            fired: false,
        }
        Object.assign(this, b)
    }
    move(game) {
        if (this.fired) {
            // 碰撞边界检测
            if (this.x < 0 || this.x > 1000 - this.w) {
                this.speedX *= -1
            }
            if (this.y < 0) {
                this.speedY *= -1
            }
            if (this.y > 500 - this.h) {
                // 游戏结束
                game.state = game.state_GAMEOVER
            }
            // 移动
            this.x -= this.speedX
            this.y -= this.speedY
        }
    }
}
// 砖块对象 
class Block {
    // 构造方法，type参数默认值为 1
    constructor(x, y, type = 1) {
        let bk = {
            x: x,
            y: y,
            w: 50,
            h: 20,
            image: type == 1 ? imageFromPath(allImg.block1) : imageFromPath(allImg.block2),
            life: type,
            alive: true,
        }
        Object.assign(this, bk)
    }
    // 消除砖块
    kill() {
        this.life--
        if (this.life == 0) {
            this.value = false
        } else if (this.life == 1) {
            this.image = imageFormPath(allImage.block1)
        }
    }
    // 小球、砖块碰撞检测
    collide(ball) {
        let b = ball
        let bk = this
        if (Math.abs((b.x + b.w/2) - (bk.x + bk.w/2)) < (b.w + bk.w)/2 &&
            Math.abs((b.y + b.h/2) - (bk.y + bk.h/2)) < (b.h + bk.h)/2) {
            this.kill()
            return true
        }
        return false  
    }
    // 计算小球、砖块碰撞后x轴速度方向
    collideBlockHorn(ball) {
        let b = ball
        let bk = this
        let rangeX = 0
        let rangeY = 0
        rangeX = Math.abs((b.x + b.w/2) - (bk.x + bk.w/2))
        rangeY = Math.abs((b.y + b.h/2) - (bk.y + bk.h/2))
        if (rangeX > bk.w/2 && rangeX < (bk.w/2 + b.w/2) && rangeY < (bk.h/2 + b.h/2)) { 
            // X轴方向与砖块四角相交
            if (b.x < bk.x && b.speedX > 0 || b.x > bk.x && b.speedX < 0) { 
                // 小球在砖块左侧时
                return false
            } else { // 小球在砖块右侧
                return true
            }
        }
        return false
    }
}
// 计分板
class Score {
    constructor(_main) {
        let s = {
            x:_main.score_x,
            y:_main.score_y,
            text: '分数: ',
            textLv: '关卡: ',
            score: 100,
            allScore: 0,
            blockList: _main.blockList,
            blockListLen: _main.blockList.length,
            lv: _main.LV,
        }
        Object.assign(this, s)
    }
    // 计算总分
    computeScore() {
        let num = 0
        let allNum = this.blockListLen
        num = this.blockListLen - this.blockList.length
        this.allScore = this.score * num
    }
}
// 定义场景
class Scene {
    // 构造方法
    constructor(lv) {
        let s = {
            lv: lv,
            canvas: document.getElementById("canvas"),
            blockList: [],
        }
        // 将 s 的内容合并到 this 中
        Object.assign(this, s)
    }
    // 实例化所有砖块对象
    initBlockList() {
        this.creatBlockList()
        let arr = []
        for (let line of this.blockList) {
            // 遍历每一行的每一个 item
            for (let item of line) {
                if (item.type == 1) {
                    let obj = new Block(item.x, item.y, 1)
                    arr.push(obj)
                }
                if (item.type == 2) {
                    let obj = new Block(item.x, item.y, 2)
                    arr.push(obj)
                }
            }
        }
        return arr
    }
    


    // 创建砖块坐标二维数组， 并生成不同关卡
    creatBlockList() {
        let lv = this.lv,
            c_w = this.canvas.width,
            c_h = this.canvas.height,
            xNum_max = c_w/50, 
            yNum_max = 12,              // y轴砖块最大数量
            x_start = 0,                // x轴起始坐标根据砖块数量而定
            y_start = 60                // y轴起始坐标，默认从60起
        // 根据 关卡 lv 添加位置数组到 blockList
        switch(lv) {
            case 1: // 三角形
                var xNum = 16,          // x轴砖块数量（第一层）    
                    yNum = 9            // y轴砖块层数
                for (let i = 0; i < yNum; i++) {
                    // 砖块位置数组
                    let arr = []
                    // 修改x轴数量
                    if (i == 0) {
                        xNum = 1
                    } else if (i == 1) {
                        xNum = 2
                    } else {
                        xNum += 2
                    }
                    x_start = (xNum_max - xNum)/2 * 50
                    // 循环x轴
                    for (let k = 0; k < xNum; k++) {
                        if (i < 3) {
                            // 前三排为特殊砖块
                            arr.push({
                                x: x_start + k * 50,
                                y: y_start + i * 20,
                                type: 2,
                            }) 
                        } else {
                            arr.push({
                                x: x_start + k * 50,
                                y: y_start + i * 20,
                                type: 1,
                            })
                        }
                    }
                    this.blockList.push(arr)
                }
        }
    }
}
